#include "ucconfigwidget.h"
#include "readonlydelegate.h"

extern QList<DevConf> confDevList;
extern Channel chn[2];
extern bool parseDev(QString filepath);
extern bool parseDatamap();

UcConfigWidget::UcConfigWidget(QWidget *parent) :
    QWidget(parent)
{

    setupUi(this);
    parseDev("./config/dev.cfg");
   // parseDatamap();
    mDevModel=new DevUCItemModel;
    mDevModel->setDatas(confDevList);
    mDevTable->setModel(mDevModel);

    mDevTable->setItemDelegateForColumn(0,new readonlyDelegate(this));
    mDevTable->setItemDelegateForColumn(1,new readonlyDelegate(this));

    connect(mDevModel,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(devNumChanged(QModelIndex,QModelIndex)));
    connect(pushButton,SIGNAL(clicked()),this,SLOT(createProject()));

}

void UcConfigWidget:: devNumChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    int row=topLeft.row();
    int column=topLeft.column();
    QVariant val=topLeft.data();

    if(column==2)
    {
        confDevList[row].num=val.toInt();
        if(val.toString()!="")
        {
            QMessageBox::information(this,"提示","设置成功");
        }
    }

}

void UcConfigWidget::createProject()
{
    Project project;

    int ret=QMessageBox::question(NULL, "提示", "你确认要生成工程么", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if(ret==QMessageBox::Yes)
    {
        project.iedname=iedNameText->text().trimmed();
        project.baud=chbxBaud->currentText().toInt();
        project.project_name=proNameText->text().trimmed();
        project.remark=remarkText->toPlainText().trimmed();

        QDir dir;
        QString dirPath="./uc7100/";
        QString filePath="";

        path.append(project_name);
        if(!dir.exists(dirPath))
        {
            dir.mkdir(dirPath);
        }

        filePath.append(dirpath).append("/").append("project.ini");


        QFile file(filePath);
        if(!file.open(QIODevice::WriteOnly))
        {
            qDebug("创建box.cfg时,无法打开文件");
            return false;
        }

        QTextStream out(&file);
        out.setCodec("utf-8");

        QString str;
        str.append("ftpip\t").append(boxInfo.dfip).append("\n")
                //.append("ip1\t").append(boxInfo.ip1).append("\n")
                //.append("ip2\t").append(boxInfo.ip2).append("\n")
                .append("user\t").append(boxInfo.user).append("\n")
                .append("psw\t").append(boxInfo.psw).append("\n");
        out<<str;
        file.close();
    }

}

DevUCItemModel::DevUCItemModel(QObject *parent):QStandardItemModel(parent)
{
    this->initProperty();
    //this->setData();
}

DevUCItemModel::~DevUCItemModel()
{

}

void DevUCItemModel::initProperty()
{
    this->setColumnCount(3);
    QStringList list;
    list<<"装置类型"<<"装置名称"<<"数量";
    this->setHorizontalHeaderLabels( list);
}

QVariant DevUCItemModel::data(const QModelIndex & index,int role) const
{
    int column=index.column();
    int row=index.row();

    if(role==Qt::DisplayRole)
    {
        if(column==0)
            return datas.at(row).dev_type;
        if(column==1)
            return datas.at(row).dev_name;
        //return 0;
    }


    return QStandardItemModel::data(index,role);
}

void DevUCItemModel::setDatas(QList<DevConf> datas)
{
    this->removeColumns(0,this->columnCount());
    this->setRowCount(datas.size());
    this->setColumnCount(3);
    this->datas=datas;
}

